#include <bits/stdc++.h>

#define eb emplace_back
#define ep emplace
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

template < typename T > T read() {
	T x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int N = 2e5 + 10;
const int V = (N << 1) - 2;

int m, n;
int a[5][N];

namespace sub1 {
	int f(int x, int y) {
		int mn = 0x3f3f3f3f, mx = -1;
		rep(k, 1, m) chkmin(mn, a[k][x] + a[k][y]), chkmax(mx, a[k][x] + a[k][y]);
		return mn + mx;
	}
	void solve() {
		ll ans = 0; rep(i, 1, n) rep(j, 1, n) ans += f(i, j);
		printf("%lld\n", ans);
	}
}

namespace sub2 {
	void solve() {
		ll ans = 0;
		rep(i, 1, m) rep(j, 1, n) ans += 1ll * a[i][j] * n * 2;
		printf("%lld\n", ans);
	}
}

namespace sub3 {
	struct elet { 
		int tp, a[3]; // 0 : add, > 1, query * pct
		elet() { tp = 0; a[0] = a[1] = a[2] = 0; }
	} ele[N << 1];
	int tot;
	ll tr[N << 2], ans;
	void add(int x, ll v) { x += N; for(; x <= V; x += x & -x) tr[x] += v; }
	ll query(int x) { x += N; ll res = 0; for(; x; x -= x & -x) res += tr[x]; return res; }
	void divide(int l, int r) {
		if(l == r) return; int mid = l + r >> 1;
		divide(l, mid); divide(mid + 1, r);
		int p = l, q = mid + 1;
		while(p <= mid && q <= r) {
			if(ele[p].a[1] <= ele[q].a[1]) {
				if(ele[p].tp == 0) add(ele[p].a[2], 1);
				p++;
			} else {
				if(ele[q].tp) ans += 1ll * ele[q].tp * query(ele[q].a[2]);
				q++;
			}
		}
		for(; q <= r; q++) if(ele[q].tp) ans += 1ll * ele[q].tp * query(ele[q].a[2]);
		for(p--; p >= l; p--) if(ele[p].tp == 0) add(ele[p].a[2], -1);
		inplace_merge(ele + l, ele + mid + 1, ele + r + 1,
					  [](auto x, auto y) {
						  return
							  x.a[1] ^ y.a[1] ? x.a[1] < y.a[1] :
							  x.a[2] ^ y.a[2] ? x.a[2] < y.a[2] :
							  x.a[0] ^ y.a[0] ? x.a[0] < y.a[0] : x.tp < y.tp;
					  });
	}
	void tsolve(int vp) {
		rep(r, 1, m) {
			tot = 0;
			rep(i, 1, n) {
				++tot; rep(j, 0, 2) ele[tot].a[j] = 0;
				rep(j, 1, m) if(j != r) ele[tot].a[j - (j > r) - 1] = a[j][i] - a[r][i] - (j > r) * (vp ? 1 : -1);
				ele[tot].tp = 0; ++tot; rep(j, 0, 2) ele[tot].a[j] = 0;
				rep(j, 1, m) if(j != r) ele[tot].a[j - (j > r) - 1] = a[r][i] - a[j][i];
				ele[tot].tp = a[r][i];
			}
			if(vp) { rep(i, 1, tot) rep(j, 0, 2) ele[i].a[j] = -ele[i].a[j]; }
			sort(ele + 1, ele + tot + 1,
				 [](auto x, auto y) {
					 return
						 x.a[0] ^ y.a[0] ? x.a[0] < y.a[0] :
						 x.a[1] ^ y.a[1] ? x.a[1] < y.a[1] :
						 x.a[2] ^ y.a[2] ? x.a[2] < y.a[2] : x.tp < y.tp;
				 });
			divide(1, tot);
		}
	}
	void solve() {
		tsolve(1); tsolve(0); printf("%lld\n", ans * 2);
	}
}

int main() {
	m = in, n = in;
	rep(i, 1, m) rep(j, 1, n) a[i][j] = in;
	if(1 && n <= 3000) sub1 :: solve();
	else if(1 && m == 2) sub2 :: solve();
	else sub3 :: solve();
	return 0;
}
